<?php
namespace Admin\Model;
use Think\Model;
class MessageModel extends Model {
	public $table='zjy_message';
	public $page; //当前页
	public $perpage; //每页显示
	public $message_id; //ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词

	//获得列表 $page当前页 $perpage每页显示 当前默认用户名 姓名
	public function get_message_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		//$where='where a.member_status=1 and a.member_id=b.member_id ';
		$where=' where 1 ';
		if($this->keyword){
			//$where.=" and (`member_email` LIKE '%".$this->keyword."%' or `member_mobile` LIKE '%".$this->keyword."%' or `member_truename` LIKE '%".$this->keyword."%' or `company_name` LIKE '%".$this->keyword."%')";
			$where.=" and (`content` LIKE '%".$this->keyword."%')";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		//$list=$Model->query("select a.* from ".$this->table." ".$where." order by member_reg_time desc limit $start,$perpage");
		//$sql="SELECT a.*,b.* FROM `zjy_member` as a ,`zjy_company` as b ".$where." order by a.member_reg_time desc limit $start,$perpage";
		$sql="SELECT * FROM ".$this->table."  ".$where." order by add_time desc limit $start,$perpage";
		//echo $sql;exit;
		$list=$Model->query($sql);
		/*$Company=M('Company');
		//print_r($list);exit;
		foreach ($list as $k=>$v){
		$list[$k]['company_name']=$Company->where('member_id='.$v['member_id'])->getField('company_name');
		}*/
		//print_r($list);exit;
		return $list;
	}

	//获得总数
	public function get_message_total()
	{
		//搜索
		//$where='where a.member_state=1 and a.member_id=b.member_id ';
		$where=' where 1 ';
		if($this->keyword){
			//$where.="and a.member_id=b.member_id and (`member_email` LIKE '%".$this->keyword."%' or `member_mobile` LIKE '%".$this->keyword."%' or `member_truename` LIKE '%".$this->keyword."%' or `company_name` LIKE '%".$this->keyword."%')";
			$where.=" and (`content` LIKE '%".$this->keyword."%')";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		//$query=$Model->query("select count(a.member_id) as total from ".$this->table." as a, zjy_company as b ".$where." ");
		$query=$Model->query("select count(message_id) as total from ".$this->table." ".$where." ");
		return $query[0]['total'];
	}

	//删除
	public function delete_message()
	{
		$ids=$this->ids;
		$Model=M("Message");
		foreach($ids as $v){
			$condition['message_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}

	/*//提交
	public function user_add_post()
	{
		$Model=M("Member");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//获得列表 $page当前页 $perpage每页显示 当前默认用户名 姓名
	public function get_user_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where a.member_state=1 and a.member_id=b.member_id ';
		if($this->keyword){
			$where.=" and (`member_email` LIKE '%".$this->keyword."%' or `member_mobile` LIKE '%".$this->keyword."%' or `member_truename` LIKE '%".$this->keyword."%' or `company_name` LIKE '%".$this->keyword."%')";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*
		$start=($page-1)*$perpage;
		$Model=new Model();
		//$list=$Model->query("select a.* from ".$this->table." ".$where." order by member_reg_time desc limit $start,$perpage");
		$sql="SELECT a.*,b.* FROM `zjy_member` as a ,`zjy_company` as b ".$where." order by a.member_reg_time desc limit $start,$perpage";
		//echo $sql;exit;
		$list=$Model->query($sql);
		$Company=M('Company');
		//print_r($list);exit;
		foreach ($list as $k=>$v){
		$list[$k]['company_name']=$Company->where('member_id='.$v['member_id'])->getField('company_name');
		}
		//print_r($list);exit;
		return $list;
	}
	//获得总数
	public function get_user_total()
	{
		//搜索
		$where='where a.member_state=1 and a.member_id=b.member_id ';
		if($this->keyword){
			$where.="and a.member_id=b.member_id and (`member_email` LIKE '%".$this->keyword."%' or `member_mobile` LIKE '%".$this->keyword."%' or `member_truename` LIKE '%".$this->keyword."%' or `company_name` LIKE '%".$this->keyword."%')";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*
		$Model=new Model();
		$query=$Model->query("select count(a.member_id) as total from ".$this->table." as a, zjy_company as b ".$where." ");
		return $query[0]['total'];
	}
	//获取信息
	public function get_user_info()
	{
		$member_id=intval($this->member_id);
		$Model=M("Member");
		$info=$Model->where('member_id='.$member_id)->find();
		return $info;
	}
	//编辑提交
	public function user_edit_post()
	{
		$Model=M("Member");
		$condition['member_id'] = $this->post['member_id'];
		$data=$this->post;
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function delete_user()
	{
		$ids=$this->ids;
		$Model=M("Member");
		foreach($ids as $v){
			$condition['member_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}

	//获取所有用户列表，用于 添加供货信息
	public function get_member_list(){
		$Model=M('Member');
		$list=$Model->field('member_id,member_truename,member_company')->where("member_state=1 and member_truename!=''")->order('member_sort asc')->select();
		//print_r($list);exit;
		return $list;
	}

	//用于统计中的会员数据 type 0全部 1年 2月 3周 4日
	public function get_member_total($type=0){
		$Model=M('Member');
		if($type==0){
			$start_time=1;
		}
		if($type==1){
			$start_time=mktime(0,0,0,1,1,date('Y')); //年
		}elseif($type==2){
			$start_time=mktime(0,0,0,date('m'),1,date('Y')); //月
		}elseif($type==3){
			$start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')); //周
		}
		elseif($type==4){
			$start_time=mktime(0,0,0,date('m'),date('d'),date('Y'));  //日
		}
		$where='member_state=1 and member_reg_time>='.$start_time;
		$count=$Model->where($where)->count();
		return $count;
	}*/

}